Passed
Push — develop ( 9011bf...5a7fee )
by Endre
03:38
created

ModuleLoader.loadModule   A

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 14
dl 0
loc 15
ccs 5
cts 5
cp 1
crap 2
rs 9.7
c 0
b 0
f 0
1
import React from 'react';
2
import {IObserver} from '../Observer/Observer';
3
4
interface ILoadedModuleDictionary {
5
  [name: string]: typeof React.Component,
6
}
7
8
export default class ModuleLoader {
9
  moduleState: IObserver<typeof React.Component | null>;
10
  moduleNameState: IObserver<string>;
11
  dictionary: ILoadedModuleDictionary;
12
  pathToRoot: string;
13
14
  constructor(
15
    pathToRoot: string,
16
    moduleNameState: IObserver<string>,
17
    moduleState: IObserver<typeof React.Component | null>
18
  ) {
19 3
    this.moduleNameState = moduleNameState;
20 3
    this.moduleState = moduleState;
21 3
    this.dictionary = {};
22 3
    this.pathToRoot = pathToRoot;
23
24 3
    this.moduleNameState.adapter.onChange = this.loadModule.bind(this);
25
  }
26
27
  async loadModule(oldValue: string, newValue: string) {
28
    let module: typeof React.Component;
29
30 3
    if (!this.dictionary.hasOwnProperty(newValue)) {
31 2
      module =
32
        (
33
          await import(this.pathToRoot + newValue + '.js')
34
        ).default as typeof React.Component;
35 2
      this.dictionary[newValue] = module;
36
    } else {
37 1
      module = this.dictionary[newValue];
38
    }
39
40 3
    this.moduleState.value = module;
41
  }
42
}